//
//  LeftViewController.m
//  囧途
//
//  Created by fly on 16/3/3.
//  Copyright © 2016年 fly. All rights reserved.
//

#import "LeftViewController.h"

#import "ControllerManger.h"
#import "LeftMenuTableCell.h"
#import "LeftMenuTableHeader.h"
#import "LoginViewController.h"
#import "SettingTableViewController.h"
#import "PersonalMessageViewController.h"
#import "MyOrderViewController.h"
#import "InfoTableViewController.h"

#import "JTPushTool.h"

#import "UIImageView+WebCache.h"
#import <AVUser.h>
#import <AVFile.h>

#import "JTUser.h"

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate, LeftMenuTableHeaderDelegate>

@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (weak, nonatomic) IBOutlet UIImageView *bgImage;

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    LeftMenuTableHeader *header = [LeftMenuTableHeader headerWithUser:[JTUser currentUser]];
    header.delegate = self;
    self.menuTable.tableHeaderView = header;
    self.menuTable.rowHeight = 60;
    self.menuTable.backgroundColor = [UIColor clearColor];
    self.menuTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.menuTable.delegate = self;
    self.menuTable.dataSource = self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    LeftMenuTableHeader *header = (LeftMenuTableHeader *)self.menuTable.tableHeaderView;
    header.currentUser = [JTUser currentUser];
    [self.bgImage sd_setImageWithURL:[JTUser currentUser].iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    
    [self.menuTable reloadData];
}


#pragma marks - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LeftMenuTableCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"LeftMenuTableCell" owner:nil options:nil] lastObject];
    
    switch (indexPath.row) {
        case 0:
            cell.menuLabel.text = @"我的订单";
            cell.menuIcon.image = [UIImage imageNamed:@"侧栏_07"];
            break;
        case 1:
            cell.menuLabel.text = @"个人中心";
            cell.menuIcon.image = [UIImage imageNamed:@"侧栏_11"];
            break;
        case 2:
            cell.menuLabel.text = @"消息";
            cell.menuIcon.image = [UIImage imageNamed:@"侧栏_17"];
            if ([JTPushTool currentNotifications].count) {
                cell.notificationImage.hidden = NO;
            }
            break;
        case 3:
            cell.menuLabel.text = @"设置";
            cell.menuIcon.image = [UIImage imageNamed:@"侧栏_21"];
            break;
            
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (![AVUser currentUser]) {
        UIStoryboard *mainStoryB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController *lgVC = [mainStoryB instantiateViewControllerWithIdentifier:@"login"];
        [[[ControllerManger sharedManager] drawerVC] closeLeftDrawer];
        [[ControllerManger sharedManager].homeVC.navigationController pushViewController:lgVC animated:YES];
        return;
    }
    
    switch (indexPath.row) {
        case 0: {
            UIStoryboard *mainStoryB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            MyOrderViewController * view = [mainStoryB instantiateViewControllerWithIdentifier:@"myorder"];
            
            [[[ControllerManger sharedManager] drawerVC] closeLeftDrawer];
            
            [[ControllerManger sharedManager].homeVC.navigationController pushViewController:view animated:YES];
        }
            break;
        case 1:
        {
            PersonalMessageViewController * view = [[PersonalMessageViewController alloc]init];
            
            [[[ControllerManger sharedManager] drawerVC] closeLeftDrawer];
            
            [[ControllerManger sharedManager].homeVC.navigationController pushViewController:view animated:YES];
        }
            break;
        case 2: {
            UIStoryboard *mainStoryB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            InfoTableViewController * view = [mainStoryB instantiateViewControllerWithIdentifier:@"myInfo"];
            
            [[[ControllerManger sharedManager] drawerVC] closeLeftDrawer];
            
            [[ControllerManger sharedManager].homeVC.navigationController pushViewController:view animated:YES];
        }
            break;
        case 3:
        {
            UIStoryboard *mainStoryB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            SettingTableViewController * view = [mainStoryB instantiateViewControllerWithIdentifier:@"setting"];
            
            [[[ControllerManger sharedManager] drawerVC] closeLeftDrawer];
            
            [[ControllerManger sharedManager].homeVC.navigationController pushViewController:view animated:YES]; 
        }
            break;
            
    }
}

#pragma mark - LeftMenuTableHeaderDelegate

- (void)didClickIcon:(LeftMenuTableHeader *)header {
    if (![AVUser currentUser]) {
        UIStoryboard *mainStoryB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController *lgVC = [mainStoryB instantiateViewControllerWithIdentifier:@"login"];
        [[[ControllerManger sharedManager] drawerVC] closeLeftDrawer];
        [[ControllerManger sharedManager].homeVC.navigationController pushViewController:lgVC animated:YES];
    } else {
        NSLog(@"已登录");
    }
}

@end
